<template>
  <div class="app-container">
    <el-row :gutter="40">
      <el-col :sm="24" :lg="6" v-for="item in totalList" :key="item.id">
        <panel-group :data="item"></panel-group>
      </el-col>
    </el-row>
    <el-row :gutter="40" justify="space-between">
      <el-col>
        <div style="float: right">
          <el-input
            style="width: 200px"
            placeholder="请输入用户手机号"
            v-model="searchKeyWord"
            @keyup.enter.native="search"
          ></el-input>
          <el-button type="primary" style="margin-left: 10px" @click="search"
            >搜索</el-button
          >
          <el-button
            type="primary"
            style="margin: 30px 30px"
            @click="dialogVisible = true"
            >新增用户</el-button
          >
        </div>
      </el-col>
    </el-row>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column align="center" label="手机号">
        <template slot-scope="scope">{{ scope.row.username }}</template>
      </el-table-column>
      <el-table-column label="密码" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.password }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center">
        <template slot-scope="scope">{{ scope.row.address }}</template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="created_at"
        label="操作"
        width="250"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="upUser(scope.row.id)"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" @click="deleteUser(scope.row.id)"
            >删除</el-button
          >
          <el-button type="info" size="mini">查看订单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="float: right; margin: 30px 0"
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="currentChange"
    >
    </el-pagination>

    <el-dialog
      title="添加用户"
      @close="diaClose"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form :model="form">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="用户密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="位置">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import PanelGroup from "@/components/PanelGroup";
import {
  userList,
  addUser,
  delUser,
  updateUser,
  searchUser,
  getUserinfo,
} from "@/api/user";
export default {
  data() {
    return {
      list: null,
      listLoading: true,
      dialogVisible: false,
      isAdd: true,
      userId: 0,
      searchKeyWord: "",
      form: {
        username: "",
        password: "",
        address: "",
      },
      //分页
      total: 1000,
      page: 1,
      totalList: [
        {
          icon: "el-icon-star-on",
          title: "今日新增人数",
          total: "102",
        },
        {
          icon: "el-icon-star-on",
          title: "本月新增人数",
          total: "102",
        },
        {
          icon: "el-icon-star-on",
          title: "总数量",
          total: "3000",
        },
      ],
    };
  },
  created() {
    this.fetchData(this.page);
  },
  methods: {
    async fetchData(page) {
      this.listLoading = true;
      let res = await userList({ page });
      this.list = res.data.rows;
      this.total = res.data.count;
      this.listLoading = false;
    },
    // 添加用户 || 修改用户
    async handleUser() {
      let bool = false;
      Object.keys(this.form).forEach((key) => {
        if (!this.form[key].length > 0) {
          bool = true;
        }
      });
      if (bool) {
        this.$message({
          message: "请检查用户信息是否填写完整",
          type: "warning",
        });
      } else {
        if (this.isAdd) {
          let result = await addUser(this.form);
          if (result.code == 200) {
            this.$message({
              message: result.msg,
              type: "success",
            });
            this.fetchData(this.page);
            this.dialogVisible = false;
          } else {
            this.$message({
              message: result.msg,
              type: "warning",
            });
          }
        } else {
          // this.dialogVisible = true;
          let result = await updateUser({ id: this.userId, ...this.form });
          if (result.code == 200) {
            this.$message({
              message: result.msg,
              type: "success",
            });
            this.fetchData(this.page);
            this.dialogVisible = false;
          } else {
            this.$message({
              message: result.msg,
              type: "warning",
            });
          }
        }
      }
    },
    currentChange(e) {
      this.page = e;
      this.fetchData(this.page);
    },
    async search() {
      if (this.searchKeyWord.trim().length > 0) {
        let result = await searchUser({ username: this.searchKeyWord.trim() });
        this.list = result.data;
      } else {
        this.fetchData(this.page);
      }
    },
    async upUser(id) {
      this.userId = id;
      this.isAdd = false;
      let userinfo = await getUserinfo({ id });
      this.form = {
        username: userinfo.data[0].username,
        password: userinfo.data[0].password,
        address: userinfo.data[0].address,
      };
      this.dialogVisible = true;
    },
    deleteUser(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        let result = await delUser({ id });
        if (result.code == 200) {
          this.$message({
            type: "success",
            message: result.msg,
          });
          this.fetchData(this.page);
        } else {
          this.$message({
            type: "error",
            message: result.msg,
          });
        }
      });
    },
    diaClose() {
      this.form = {
        username: "",
        password: "",
        address: "",
      };
    },
  },
  components: {
    PanelGroup,
  },
};
</script>
